package com.windward.vii.web.controller;

import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.dao.entity.AssetUserAccount;
import com.windward.vii.dao.entity.Blacklist;
import com.windward.vii.domain.user.UserVO;
import com.windward.vii.service.BlacklistService;
import com.windward.vii.service.UserTokenService;
import com.windward.vii.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class BlacklistController {

    private static Logger logger = LoggerFactory.getLogger(BlacklistController.class);

    @Resource
    UserTokenService userTokenService;
    @Resource
    BlacklistService blacklistService;

    @RequestMapping(value = "v1/api1/user/black/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult add(@RequestParam Long targetUid,
                              @RequestHeader(value="token") String token) {
        if(targetUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "targetUid错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        if(targetUid == uid) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "targetUid错误，不能拉黑自己");
        }
        Blacklist userBlacklist = new Blacklist();
        userBlacklist.setUid(uid);
        userBlacklist.setBlackUid(targetUid);
        userBlacklist.setCreateTime(new Date());
        boolean result = blacklistService.save(userBlacklist);
        return result ? JsonResult.success() : JsonResult.error();
    }

    @RequestMapping(value = "v1/api1/user/black/cancel", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult cancel(@RequestParam Long targetUid,
                             @RequestHeader(value="token") String token) {
        if(targetUid <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "targetUid错误");
        }
        Long uid = userTokenService.getUidByToken(token);
        if(targetUid == uid) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "targetUid错误，不能操作自己");
        }
        boolean result = blacklistService.deleteByUidAndBlackUid(userTokenService.getUidByToken(token), targetUid);
        return result ? JsonResult.success() : JsonResult.error();
    }

    @RequestMapping(value = "v1/api1/user/black/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult list(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestHeader(value="token") String token,
                           @RequestHeader(value="User-Agent") String userAgent) {
        logger.info("pageNum={}", pageNum);
        logger.info("pageSize={}", pageSize);
        pageNum = pageNum > 0 ? pageNum : 1;
        List<AssetUserAccount> userList = blacklistService.getBlackList(pageNum, pageSize, userTokenService.getUidByToken(token));
        List<UserVO> list = UserUtils.toUserVO(userList);
        for(UserVO userVO : list) {
            userVO.setIsBlack(true);
        }
        return new JsonResult(ResultCode.SUCCESS_CODE, ResultCode.SUCCESS_MSG, list);
    }


}
